package com.meijm.entity;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

public class WeiBo  implements PageProcessor {
	private Site site = Site
			.me()
			.setDomain("m.weibo.cn")
			.setSleepTime(3000)
			.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");

	public void process(Page page) {
		String totalStr = page.getJson().jsonPath("$.data.cardlistInfo.total").get();
		String pageStr = page.getJson().jsonPath("$.data.cardlistInfo.page").get();

		List<String> userCount = page.getJson().jsonPath("$.data.cards[*].mblog.user.screen_name").all();
		List<String> weibos = page.getJson().jsonPath("$.data.cards[*].mblog.text").all();
		List<String> dates = page.getJson().jsonPath("$.data.cards[*].mblog.created_at").all();
		List<String> reposts = page.getJson().jsonPath("$.data.cards[*].mblog.reposts_count").all();
		List<String> comments = page.getJson().jsonPath("$.data.cards[*].mblog.comments_count").all();
		List<String> attitudes = page.getJson().jsonPath("$.data.cards[*].mblog.attitudes_count").all();

		for (int j = 0; j < userCount.size(); j++) {
			page.putField("用户名",userCount.get(j));
			page.putField("微博"+j, weibos.get(j));
			page.putField("发表时间"+j, dates.get(j));
			page.putField("转发"+j, reposts.get(j));
			page.putField("评论"+j, comments.get(j));
			page.putField("点赞"+j, attitudes.get(j));
		}

		int total = 0, pageInt;
		if (totalStr != null && totalStr.length() > 0) {
			total = Integer.valueOf(totalStr);
		}
		if (pageStr != null && pageStr.length() > 0) {
			pageInt = Integer.valueOf(pageStr);
		}
		// 总页数
		int pages = (total + 10 - 1) / 10, i;
		for (i = 2; i <= pages; i++) {
			page.addTargetRequest("https://m.weibo.cn/api/container/getIndex?type=uid&value=5067385205&containerid=1076035067385205&page=" + i);
		}

	}


	public static void main(String[] args) {
		Spider.create(new WeiBo())
				.addUrl("https://m.weibo.cn/api/container/getIndex?type=uid&value=5067385205&containerid=1076035067385205&page=1")
				.addPipeline(new FilePipeline("/home/dengfengdecao/Downloads/test"))
				.thread(3)
				.run();
	}

	public Site getSite() {
		return site;
	}
}
